//
//  JOEditViewController.m
//  私人通讯录
//
//  Created by song on 15/1/23.
//  Copyright (c) 2015年 song. All rights reserved.
//

#import "JOEditViewController.h"
#import "JOContects.h"
@interface JOEditViewController ()

@end

@implementation JOEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.nameField.text = self.contect.name;
    self.telField.text = self.contect.tel;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.nameField];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textChange) name:UITextFieldTextDidChangeNotification object:self.telField];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)textChange
{
    if(self.nameField.text.length>0 && self.telField.text.length>0)
    {
        self.savebtn.enabled = YES;
    }else{
        self.savebtn.enabled =NO;
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


- (IBAction)saveclick:(id)sender {
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
    if([self.delegate respondsToSelector:@selector(EditViewControllerWithContect:)])
    {
        self.contect.name = self.nameField.text;
        self.contect.tel = self.telField.text;
        [self.delegate EditViewControllerWithContect:self.contect];
    }
    
}

- (IBAction)editclick:(UIBarButtonItem *)item {
    
    if(self.nameField.enabled)
    {
        self.nameField.text = self.contect.name;
        self.telField.text = self.contect.tel;
        self.nameField.enabled =NO;
        self.telField.enabled =NO;
        [self.view endEditing:YES];
        self.savebtn.hidden =YES;
        item.title =@"编辑";
    }else{
        self.nameField.enabled =YES;
        self.telField.enabled =YES;
        [self.telField becomeFirstResponder];
        self.savebtn.hidden =NO;
        item.title =@"取消";
    }
    

    
}
@end
